<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="发布人名称" prop="publishName">
        <el-input
          v-model="queryParams.publishName"
          placeholder="请输入发布人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon=""
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bbsComments:bbsComments:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      :height="tableHeight"
      ref="tableRef"
      v-if="refreshTable"
      v-loading="loading"
      :data="bbsCommentsList"
      row-key="id"
      lazy
      :load="load"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
<!--      <el-table-column label="帖子ID" prop="bbsId" />-->
      <el-table-column label="发布人名称" align="center" prop="publishName" />
      <el-table-column label="发布人头像" align="center" prop="publishHead" >
        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.publishHead">
        </template>
      </el-table-column>
      <el-table-column label="正文" prop="mainBody" :show-overflow-tooltip="true"/>
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="图片列表" align="center" prop="imageList" >-->
<!--        <template slot-scope="scope">-->
<!--          <img style="width:60px;height:60px;border:none;" :src="scope.row.imageList">-->
<!--        </template>-->
<!--      </el-table-column>-->
      <!--<el-table-column label="发布端" align="center" prop="publish">
        <template slot-scope="scope">
          <dict-tag :options="publishOptions" :value="scope.row.publish"/>
        </template>
      </el-table-column>-->
<!--      <el-table-column label="发布人id" align="center" prop="publishId" />-->
<!--      <el-table-column label="上级id" align="center" prop="parentId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bbsComments:bbsComments:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon=""
            @click="handleAdd(scope.row)"
            v-hasPermi="['bbsComments:bbsComments:add']"
          >新增</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleReply(scope.row.bbsId,scope.row.id)"
          >回复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bbsComments:bbsComments:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改论坛评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="上级id" prop="parentId">
          <treeselect v-model="form.parentId" :options="bbsCommentsOptions" :normalizer="normalizer" placeholder="请选择上级id" />
        </el-form-item> -->
        <el-form-item label="发布人名称"  prop="publishName">
          <el-input v-model="form.publishName" placeholder="请输入发布人名称" />
        </el-form-item>
        <el-form-item label="发布人头像">
<!--          <imageUpload v-model="form.publishHead"/>-->
          <image-upload  :limit="1" v-model="form.publishHead" />
        </el-form-item>
        <el-form-item label="正文" prop="mainBody">
          <el-input v-model="form.mainBody" type="textarea" placeholder="请输入内容" />
        </el-form-item>
       <!-- <el-form-item label="所属端" prop="belongs">
          <el-input v-model="form.belongs" placeholder="请输入所属端" />
        </el-form-item>
        <el-form-item label="图片列表">
          <imageUpload v-model="form.imageList"/>
        </el-form-item>-->
   <!--     <el-form-item label="发布端" prop="publish">
          <el-select v-model="form.publish" placeholder="请选择发布端">
            <el-option
              v-for="dict in publishOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>-->
<!--        <el-form-item label="帖子ID" prop="bbsId">-->
<!--          <el-input v-model="form.bbsId" placeholder="请输入帖子ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="发布人id" prop="publishId">-->
<!--          <el-input v-model="form.publishId" placeholder="请输入发布人id" />-->
<!--        </el-form-item>-->

  <!--      <el-form-item label="被点赞数" prop="likeNum">
          <el-input v-model="form.likeNum" placeholder="请输入发布人头像" />
        </el-form-item>
        <el-form-item label="创建来源" prop="createType">
          <el-select v-model="form.createType" placeholder="请选择创建来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="更新来源" prop="updateType">
          <el-select v-model="form.updateType" placeholder="请选择更新来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 回复 -->
    <el-dialog title="回复" :visible.sync="reply" width="500px" append-to-body>
      <el-form ref="form" :model="replyform" label-width="100px">
        <el-form-item label="发布人名称"  prop="publishName">
          <el-input v-model="replyform.publishName" placeholder="请输入发布人名称" />
        </el-form-item>
        <el-form-item label="发布人头像">
          <image-upload  :limit="1" v-model="replyform.publishHead" />
        </el-form-item>
        <el-form-item label="正文" prop="mainBody">
          <el-input v-model="replyform.mainBody" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReply">确 定</el-button>
        <el-button @click="cancelReply">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBbsComments, getBbsComments, delBbsComments, addBbsComments, updateBbsComments, exportBbsComments,replyBbsComments } from "@/api/bbsComments/bbsComments";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "BbsComments",
  components: {
    Treeselect
  },
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 论坛评论表格数据
      bbsCommentsList: [],
      // 论坛评论树选项
      bbsCommentsOptions: [],
      // 弹出层标题
      title: "",
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      //回复弹出层
      reply:false,
      // 发布端字典
      publishOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bbsId: null,
        mainBody: null,
        belongs: null,
        imageList: null,
        publish: null,
        publishId: null,
        publishName: null,
        publishHead: null,
        likeNum: null,
        parentId: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      replyform: {},
      // 表单校验
      rules: {
      }
    };
  },
  beforeRouteEnter(to, form, next) {
    next(vm => {
      const bbsId = vm.$route.query && vm.$route.query.id;
      vm.queryParams.bbsId = bbsId;
      vm.getList();
    });
  },
  created() {
    //this.getList();
    this.calHeight();
    this.getDicts("publish").then(response => {
      this.publishOptions = response.data;
    });

  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    calHeight () {
      this.$nextTick(() => {
        const rect = this.$refs.appcontainer.getBoundingClientRect();
        let height= this.$refs.queryForm.$el.offsetHeight;
        this.tableHeight = rect.height-height-120;
      })
    },
    //延迟加载
    load(tree, treeNode, resolve) {
      this.queryParams.parentId=tree.id;
      this.loading = true;
      listBbsComments(this.queryParams).then(response => {
        let  chlidCommentList = this.handleTree(response.rows, "id", "parentId");
        resolve(chlidCommentList);
        this.loading = false;
      });
    },
    /** 查询论坛评论列表 */
    getList() {
      this.loading = true;
      listBbsComments(this.queryParams).then(response => {
        this.bbsCommentsList = this.handleTree(response.rows, "id", "parentId");
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换论坛评论数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.publishName,
        children: node.children
      };
    },
	/** 查询论坛评论下拉树结构 */
    getTreeselect() {
      listBbsComments().then(response => {
        this.bbsCommentsOptions = [];
        const data = { id: 0, publishName: '顶级节点', children: [] };
        data.children = this.handleTree(response.rows, "id", "parentId");
        this.bbsCommentsOptions.push(data);
      });
    },
    handleReply(bbsid,id) {
      this.reply = true
      this.replyform.bbsId = bbsid
      this.replyform.parentId = id
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelReply() {
      this.reply = false
      this.replyform = {}
    },
    submitReply() {
      const formdata = new FormData()
      for(let i in this.replyform) {
        formdata.append(i,this.replyform[i])
      }
      // const data = this.replyform
      replyBbsComments(formdata).then(res => {
        if(res.code == 200) {
          this.msgSuccess('回复成功')
          this.reply = false
          this.getList();
          this.replyform = {}
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bbsId: null,
        mainBody: null,
        belongs: null,
        imageList: null,
        publish: null,
        publishId: null,
        publishName: null,
        publishHead: null,
        likeNum: null,
        parentId: null,
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
        createBy: null,
        createType: null,
        createTime: null,
        updateBy: null,
        updateType: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加论坛评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getBbsComments(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改论坛评论";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBbsComments(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.queryParams.parentId=0;
              this.getList();
              this.updateNodeChild(this.form.parentId);
            });
          } else {
            this.form.bbsId = this.queryParams.bbsId;

            addBbsComments(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.queryParams.parentId=0;
              this.getList();
              this.updateNodeChild(this.form.parentId);
            });
          }
        }
      });
    },
    updateNodeChild(pid){
      this.queryParams.parentId=pid;
      this.loading = true;
      listBbsComments(this.queryParams).then(response => {
        let bbsCommentsList= this.handleTree(response.rows, "id", "parentId");
        this.loading = false;
        this.$set(this.$refs.tableRef.store.states.lazyTreeNodeMap,pid,bbsCommentsList)
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除论坛评论?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBbsComments(row.id);
        }).then(() => {
          this.queryParams.parentId=0;
          this.getList();
          this.updateNodeChild(row.parentId);
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
  }
};
</script>
<style scoped>
  .el-tooltip__popper{
    max-width:20%;
  }
  .el-tooltip__popper,.el-tooltip__popper.is-dark{
    background:rgb(48, 65, 86) !important;
    color: #fff !important;
    line-height: 24px;
  }
</style>
